package _4显示图片;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class ImageDemoFrame extends JFrame implements ActionListener{

	JLabel l2;
	JLabel l1;
	
	ImageIcon img1 = new ImageIcon("./image/shitou.png");
	ImageIcon img2 = new ImageIcon("./image/jianzi.png");
	ImageIcon img3 = new ImageIcon("./image/bu.png");
	
	public ImageDemoFrame()
	{
		setTitle("石头剪刀布");
		setBounds(100, 100, 300, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setLayout(null);
		
		
		JLabel l1 = new JLabel(img1);
		this.add(l1);
		l1.setBounds(100,10,100,100);
		
		l2 = new JLabel(img1);
		this.add(l2);
		l2.setBounds(100,120,100,100);
		
		JButton btn1 = new JButton(img1);
		btn1.setActionCommand("1");
		JButton btn2 = new JButton(img2);
		btn2.setActionCommand("2");		
		JButton btn3 = new JButton(img3);
		btn3.setActionCommand("3");
		this.add(btn1);
		this.add(btn2);
		this.add(btn3);
		btn1.setBounds(0,250,100,100);
		btn2.setBounds(100,250,100,100);
		btn3.setBounds(200,250,100,100);
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
	}
	
	public static void main(String[] args) {
		ImageDemoFrame f = new ImageDemoFrame();
		f.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		System.out.println(e.getActionCommand());
		switch(e.getActionCommand())
		{
		case "1":
			l2.setIcon(img1);
			break;
		case "2":
			l2.setIcon(img2);
			break;
		case "3":
			l2.setIcon(img3);
			break;
			
		}
		
		Random r= new Random();
		int computer =r.nextInt(3)+1 ;
		switch(computer)
		{
		case 1:
			l1.setIcon(img1);
			break;
		case 2:
			l1.setIcon(img2);
			break;
		case 3:
			l1.setIcon(img3);
			break;
		
		}
		
	}
}
